package main;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import load.LoadData;
import db.CreateTables;
import db.DatabaseConnector;

/**
 * This class is used to run the loader.
 * 
 * @author Brad Hanlon
 *
 */
public class Main {

	/**
	 * Connects to the database, creates the tables, and loads the data.
	 * 
	 * @param args
	 */
	public static void main(String[] args){
		
		// Create a new properties object from the csv.properties file
		// csv.properties the directory location of the different csv
		// files that are going to be used.
		Properties prop = new Properties();
		try{
			FileInputStream fis = new FileInputStream("csv.properties");
			prop.load(fis);
		}
		catch(IOException e){
			e.printStackTrace();
			System.exit(1);
		}
		
		// Connect to the database
		DatabaseConnector db = new DatabaseConnector();
		db.connect();		
		
		// Drop all the tables
		System.out.println("Dropping tables");
		CreateTables.dropTables(db.getConnection());
		System.out.println("Tables dropped");
		
		// Create all the tables
		System.out.println("Creating tables");
		CreateTables.createAllTables(db.getConnection());
		System.out.println("Tables created");
		
		// Load all the data
		LoadData loader = new LoadData(db.getConnection(), prop);
		loader.LoadAll();
		
		// Disconnect from the database
		// REMEMBER: ALWAYS disconnect from the database
		db.disconnect();
	}
	
}
